package com.goblin.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main implements Runnable {

    private Object lock1;
    private Object lock2;
    private String content;
    private int count;

    public Main(Object lock1, Object lock2, String content, int count) {
        this.lock1 = lock1;
        this.lock2 = lock2;
        this.content = content;
        this.count = count;
    }

    @Override
    public void run() {
        while (count > 0) {
            synchronized (lock1) {
                synchronized (lock2) {
                    System.out.print(content);
                    count--;
                    lock2.notify();
                    System.out.println(Thread.currentThread().getName());
                    System.out.println("释放锁2" + lock2.toString());


                    try {
                        System.out.println(Thread.currentThread().getName());
                        System.out.println("锁1等待" + lock1.toString());
                        lock1.wait();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {

        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        Object d = new Object();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String line = br.readLine();

        int count = Integer.parseInt(line);


        Main tA = new Main(d, a, "A", count);
        Main tB = new Main(a, b, "B", count);
        Main tC = new Main(b, c, "C", count);
        Main tD = new Main(c, d, "D", count);


        new Thread(tA).start();
        Thread.sleep(100);
        new Thread(tB).start();
        Thread.sleep(100);
        new Thread(tC).start();
        Thread.sleep(100);
        new Thread(tD).start();


    }
}